var bindMobile = {
		timers:	180,
		tipId: "",
		//初始化
		init: function(){
			//手机号修改事件
			$(".regMobile").keyup(function(){
				var regMobile = $(this).val();
				var reg = new RegExp("^1[345789]\\d{9}$");
	    		if (reg.test(regMobile)) {
	    			$(".sendSms").removeClass("disabled");
	    		}
			});
			//发送短息
			$(".sendSms").click(function(){
				
				var $that = $(this);
				if($that.hasClass("disabled")){
					return false;
				}
				$(".err-msg").hide();
				$(".imageCode").parent("div").removeClass("has-error");
				var callCode = $(".callShow").attr("callCode");
				var imageCode = $(".imageCode").val();
				if(imageCode==""){
					$(".imageCode").parent("div").addClass("has-error");
					return false;
				}
				$.post("/sendSms",{
					mobile:$(".regMobile").val(),
					callCode:callCode,
					imageCode:imageCode
				},function(data){
					if(data.code=="200"){
						$that.addClass("disabled");
						var text = $that.text();
						bindMobile.timers=180;
						bindMobile.tipId=window.setInterval("bindMobile.timer_('"+text+"')",1000);
					}else{
						$(".err-msg").text(data.msg);
						$(".err-msg").show();
						$(".sendSms").removeClass("disabled");
					}
				},"json")
				
			});
			
			//提交
			$(".btn.btn-submit").click(function(){
				$(".err-msg").hide();
				var mobile = $(".regMobile").val();
				var smsCode = $(".smsCode").val();
				var callCode = $(".callShow").attr("callCode");
				if(mobile==""){
					$(".regMobile").parent("div").addClass("has-error");
					return false;
				}
				if(smsCode==""){
					$(".smsCode").parent("div").addClass("has-error");
					return false;
				}
				var $that = $(this);
				$.post("/user/bindMobile",{
					userId:$("#userId").val(),
					mobile:mobile,
					smsCode:smsCode,
					callCode:callCode
				},function(data){
					if(data.code=="200"){
						$(".form-base.init").hide();
						$(".form-base.success").show();
					}else{
						$(".err-msg").text(data.msg);
						$(".err-msg").show();
					}
				},"json")
				
			});
			
			$(".callCode").click(function(){
				//查询数据
				$.post("/findDictionary",{
					code:"CALL_CODE",
					text:""
				},function(data){
					$(".callItem").remove();
					var html = "";
					$.each(data,function(index,e){
						html+="<li  class=\"ng-scope callItem\" callCode=\""+e.value+"\" ><a href=\"#\" class=\"ng-binding\">"+e.text+"("+e.value+")</a></li>";
					});
					$(".callHtml").append(html);
				},"json");
			});
			
			$("body").on("click",".callItem",function(){
				var $that = $(this);
				$(".callShow").text($that.find("a").text());
				$(".callShow").attr("callCode",$that.attr("callCode"));
			});
			
			$(".select-search-content input").keyup(function(){
				//查询数据
				var $that = $(this);
				$.post("/findDictionary",{
					code:"CALL_CODE",
					text:$that.val()
				},function(data){
					$(".callItem").remove();
					var html = "";
					$.each(data,function(index,e){
						html+="<li  class=\"ng-scope callItem\" callCode=\""+e.value+"\" ><a href=\"#\" class=\"ng-binding\">"+e.text+"("+e.value+")</a></li>";
					});
					$(".callHtml").append(html);
				},"json");
			});
			
			$("#image").click(function(){
				var $that = $(this);
				$that.attr("src","/valicode?t="+new Date().getTime());
			});
			
			
		},
		timer_: function(text){
			if(bindMobile.timers>=0){
				$(".sendSms").text(bindMobile.timers+"S");
				bindMobile.timers--;
			}else{
				window.clearInterval(bindMobile.tipId);
				$(".sendSms").text(text);
				$(".sendSms").removeClass("disabled");
			}
		}
};


$(function(){
	bindMobile.init();
})

